import axios from "axios";
import { useUserStore } from "@/stores/user.js";
import router from "@/router";
const baseUrl = "https://v3pz.itndedu.com/v3pz";
const instance = axios.create({
  baseURL: baseUrl,
  timeout: 5000,
  headers: {
    terminal: "h5",
  },
});
// 请求拦截
instance.interceptors.request.use(
  (config) => {
    // const useStore = useUserStore();
    // if (useStore.token) {
    //   config.headers["Authorization"] = useStore.token;
    // }
    const token = localStorage.getItem("h5_token");
    const whiteUrl = ["/login"];
    if (token && whiteUrl.includes(config.url)) {
      config.headers["h_token"] = token;
    }
    return config;
  },
  (err) => {
    Promise.reject(err);
  }
);
// 响应拦截
instance.interceptors.response.use(
  (res) => {
    if (res.data.code === -1) {
    }
    if (res.data.code === -2) {
      localStorage.removeItem("h5_token");
      localStorage.removeItem("h5_userInfo");
    }

    return res.data;
  },
  (error) => {
    console.log(error, "error");
    // const status = error.response.status;
    // console.log(status, "status");
    // switch (status) {
    //   case 401:
    //     // 错误信息
    //     ElMessage.error("参数错误");
    //     break;
    //   case 404:
    //     // 错误信息
    //     ElMessage.error(error.response.data.message);
    //     break;
    //   case 500 | 501 | 502 | 503 | 504 | 505:
    //     // 错误信息
    //     ElMessage.error("服务器错误");
    //     break;
    // }
    return Promise.reject(new Error(error.message));
  }
);
export default instance;
export { baseUrl };
